#include "rotatedlabel.h"
#include <QPainter>

RotatedLabel::RotatedLabel(const QString &text, QWidget *parent)
        : QWidget(parent), _label(text)
{
    _angle = 0;
    _backgroundColor = palette().color(QPalette::Window);
    _label.setTextFormat(Qt::RichText);
}

void RotatedLabel::setAngle(qreal angle)
{
    if (_angle != angle) {
        _angle = angle;
        updatePixmap();
    }
}

void RotatedLabel::setText(const QString &text)
{
    if (text != _label.text()) {
        _label.setText(text);
        updatePixmap();
    }
}

void RotatedLabel::updatePixmap()
{
    _label.setFont(this->font());
    _label.setPalette(palette());
    _label.adjustSize();
    _pixmap = QPixmap(_label.size());
    _label.render(&_pixmap);
    QTransform t;
    t.rotate(_angle);
    _pixmap = _pixmap.transformed(t, Qt::SmoothTransformation);
    setMinimumSize(_pixmap.width(), _pixmap.height());
    update();
}

void RotatedLabel::updateLabel()
{
    updatePixmap();
}

QSize RotatedLabel::sizeHint() const
{
    return minimumSize();
}

QSize RotatedLabel::minimumSizeHint() const
{
    return minimumSize();
}

void RotatedLabel::resizeEvent(QResizeEvent */*event*/)
{
    updatePixmap();
}

void RotatedLabel::paintEvent(QPaintEvent */*event*/)
{ 
    QPainter painter(this);
    int x = 0;
    int y = 0;

    Qt::Alignment alignment = _label.alignment();

    switch (alignment) {

    case Qt::AlignHCenter: x = (width() - _pixmap.width())/2;
        break;
    case Qt::AlignRight: x = width() - _pixmap.width();
        break;
    case Qt::AlignVCenter: y = (height() - _pixmap.height())/2;
        break;
    case Qt::AlignBottom: y = height() - _pixmap.height();
        break;
    case Qt::AlignCenter: {
            x = (width() - _pixmap.width())/2;
            y = (height() - _pixmap.height())/2;
        } break;

    default: break;
    }

    if (alignment == (Qt::AlignLeft | Qt::AlignTop)) {
        x = 0;
        y = 0;
    }
    if (alignment == (Qt::AlignLeft | Qt::AlignVCenter)) {
        x = 0;
        y = (height() - _pixmap.height())/2;
    }
    if (alignment == (Qt::AlignLeft | Qt::AlignBottom)) {
        x = 0;
        y = height() - _pixmap.height();
    }

    if (alignment == (Qt::AlignHCenter | Qt::AlignTop)) {
        x = (width() - _pixmap.width())/2;
        y = 0;
    }
    if (alignment == (Qt::AlignHCenter | Qt::AlignBottom)) {
        x = (width() - _pixmap.width())/2;
        y = height() - _pixmap.height();
    }

    if (alignment == (Qt::AlignRight | Qt::AlignTop)) {
        x = width() - _pixmap.width();
        y = 0;
    }
    if (alignment == (Qt::AlignRight | Qt::AlignVCenter)) {
        x = width() - _pixmap.width();
        y = (height() - _pixmap.height())/2;
    }
    if (alignment == (Qt::AlignRight | Qt::AlignBottom)) {
        x = width() - _pixmap.width();
        y = height() - _pixmap.height();
    }

    painter.drawPixmap(x, y, _pixmap.width(), _pixmap.height(), _pixmap);
}

void RotatedLabel::setAlignment(Qt::Alignment alignment)
{
    if (alignment != _label.alignment()) {
        _label.setAlignment(alignment);
        updatePixmap();
    }
}

Qt::Alignment RotatedLabel::alignment() const
{
    return _label.alignment();
}

void RotatedLabel::setBackgroundColor(const QColor &color)
{
    if (color != _backgroundColor) {
        _backgroundColor = color;
        _backgroundColor.setAlpha(255);
        QPalette p = palette();
        p.setColor(QPalette::Window, _backgroundColor);
        setPalette(p);
        updatePixmap();
    }
}

QColor RotatedLabel::backgroundColor() const
{
    return _backgroundColor;
}

QString RotatedLabel::text() const
{
    return _label.text();
}

qreal RotatedLabel::angle() const
{
    return _angle;
}
